﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Logic;
using LotteryVote.Entity;

namespace LotteryVote
{
    public partial class GamePlayWayManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindGamePlayWay();
        }

        void BindGamePlayWay()
        {
            GamePlayWayFacade gpyFacade = new GamePlayWayFacade();
            ddl_gameType.DataSource = gpyFacade.GetAllGameType();
            ddl_gameType.DataTextField = GameType.GAMENAME;
            ddl_gameType.DataValueField = GameType.GAMEID;
            ddl_gameType.DataBind();
            ddl_gameType.Items.Insert(0, new ListItem("请选择", "999"));

            ddl_palyWay.DataSource = gpyFacade.GetAllPlayWay();
            ddl_palyWay.DataTextField = PlayWay.WAYNAME;
            ddl_palyWay.DataValueField = PlayWay.WAYID;
            ddl_palyWay.DataBind();
            ddl_palyWay.Items.Insert(0, new ListItem("请选择", "999"));

            ddl_gameplayWay.DataSource = gpyFacade.GetAllGamePlayWay().Select(it => new { GamePlayWayId = it.Id, Name = it.GameType.GameName + "=>" + it.PlayWay.WayName });
            ddl_gameplayWay.DataTextField = "Name";
            ddl_gameplayWay.DataValueField = "GamePlayWayId";
            ddl_gameplayWay.DataBind();

            CompanyTypeFacade comTypeFacade = new CompanyTypeFacade();
            ddl_companyType.DataSource = comTypeFacade.GetAllType();
            ddl_companyType.DataTextField = CompanyTypeModel.TYPENAME;
            ddl_companyType.DataValueField = CompanyTypeModel.ID;
            ddl_companyType.DataBind();
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            GamePlayWayFacade gpyFacade = new GamePlayWayFacade();
            int gameId = int.Parse(ddl_gameType.SelectedValue);
            int wayId = int.Parse(ddl_palyWay.SelectedValue);
            if (gpyFacade.IsExistGamePlayWay(gameId, wayId))
            {
                ltl_message.Text = "已存在!";
                return;
            }
            gpyFacade.AddGamePlayWay(gameId, wayId);
            ltl_message.Text = "添加成功!";
        }

        protected void btn_GetAll_Click(object sender, EventArgs e)
        {
            GamePlayWayFacade gpyFacade = new GamePlayWayFacade();
            var result = gpyFacade.GetAllGamePlayWayGroupByGameType();
            dl_GamePlayWay.DataSource = result;
            dl_GamePlayWay.DataBind();
        }

        protected void btn_del_Click(object sender, EventArgs e)
        {
            GamePlayWayFacade gpyFacade = new GamePlayWayFacade();
            int gameId = int.Parse(ddl_gameType.SelectedValue);
            int wayId = int.Parse(ddl_palyWay.SelectedValue);
            gpyFacade.RemoveGamePlayWay(gameId, wayId);
            ltl_message.Text = "删除成功!";
        }

        protected void btn_GetByGameType_Click(object sender, EventArgs e)
        {
            GamePlayWayFacade gpyFacade = new GamePlayWayFacade();
            int gameId = int.Parse(ddl_gameType.SelectedValue);
            gv_playWay.DataSource = gpyFacade.GetPlayWayByGameType(gameId);
            gv_playWay.DataBind();
        }

        protected void btn_AddSupport_Click(object sender, EventArgs e)
        {
            int gamePlayWayId = int.Parse(ddl_gameplayWay.SelectedValue);
            int companyTypeId = int.Parse(ddl_companyType.SelectedValue);
            GamePlayWayFacade gpwFacade = new GamePlayWayFacade();
            if (gpwFacade.IsCompanyTypeSupportWayOfPlay(companyTypeId, gamePlayWayId))
            {
                ltl_message.Text = "已存在!";
                return;
            }
            gpwFacade.AddCompanyTypeSupportWayOfPlay(companyTypeId, gamePlayWayId);
            ltl_message.Text = "添加成功!";
        }

        protected void btn_RemoveSupport_Click(object sender, EventArgs e)
        {
            int gamePlayWayId = int.Parse(ddl_gameplayWay.SelectedValue);
            int companyTypeId = int.Parse(ddl_companyType.SelectedValue);
            GamePlayWayFacade gpwFacade = new GamePlayWayFacade();
            gpwFacade.RemoveCompanyTypeSupportWayOfPlay(companyTypeId, gamePlayWayId);
            ltl_message.Text = "删除成功!";
        }

        protected void btn_GetSupport_Click(object sender, EventArgs e)
        {
            int companyTypeId = int.Parse(ddl_companyType.SelectedValue);
            GamePlayWayFacade gpwFacade = new GamePlayWayFacade();
            dl_support.DataSource = gpwFacade.GetGamePlayWayByCompanyType(companyTypeId);
            dl_support.DataBind();
        }

        GamePlayWayFacade GPWFacade = new GamePlayWayFacade();
        protected void btn_addGameType_Click(object sender, EventArgs e)
        {
            GPWFacade.AddGameType(txt_gameName.Text, txt_gameDesc.Text);
            ltl_message.Text = "添加成功";
        }

        protected void btn_updateGameType_Click(object sender, EventArgs e)
        {
            GPWFacade.UpdateGameType(int.Parse(txt_gameId.Text), txt_gameName.Text, txt_gameDesc.Text);
            ltl_message.Text = "修改成功!";
        }

        protected void btn_deleteGameType_Click(object sender, EventArgs e)
        {
            GPWFacade.RemoveGameType(int.Parse(txt_gameId.Text));
            ltl_message.Text = "删除成功";
        }

        protected void btn_addPlayWay_Click(object sender, EventArgs e)
        {
            GPWFacade.AddPlayWay(txt_wayName.Text, txt_wayDesc.Text);
            ltl_message.Text = "添加成功";
        }

        protected void btn_updatePlayWay_Click(object sender, EventArgs e)
        {
            GPWFacade.UpdatePlayWay(int.Parse(txt_wayId.Text), txt_wayName.Text, txt_wayDesc.Text);
            ltl_message.Text = "修改成功!";
        }

        protected void btn_deletePlayWay_Click(object sender, EventArgs e)
        {
            GPWFacade.RemovePlayWay(int.Parse(txt_wayId.Text));
            ltl_message.Text = "删除成功";
        }
    }
}